<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
  <title>Demo</title>
  <style>
  #paper {
    position: relative;
    width: 800px;
    height: 600px;
    background: grey;
  }
  </style>
</head>
<body>
  <div id="paper"></div>
  <script src="/js/d3.js"></script>
  <script src="/js/spritejs.js"></script>
  <script>

const paper = spritejs.Paper2D('#paper').setResolution(1200, 900)

const layer = paper.layer('fglayer', {
  handleEvent: true
})

var simulation = d3.forceSimulation()
    .force("link", d3.forceLink().id(function(d) { return d.id; }))
    .force("charge", d3.forceManyBody())
    .force("center", d3.forceCenter(400, 300));

d3.json("/data/miserables.json", function(error, graph) {
  if (error) throw error

  simulation
      .nodes(graph.nodes)
      .on("tick", ticked)

  simulation.force("link")
      .links(graph.links)

  let path = new spritejs.Path()
  path.attr({
    size: [800, 600],
    //bgcolor: '#aaa',
  })
  layer.appendChild(path)

  d3.select(layer).on('click', () => {
    const [x, y] = layer.toLocalPos(d3.event.originalX, d3.event.originalY)
    console.log(x, y)
  })

  d3.select(layer.canvas)
      .call(d3.drag()
          .container(layer.canvas)
          .subject(dragsubject)
          .on("start", dragstarted)
          .on("drag", dragged)
          .on("end", dragended))

  path.on('afterdraw', evt => {
    const {context} = evt
    graph.links.forEach(d => {
      const [sx, sy] = [d.source.x, d.source.y],
            [tx, ty] = [d.target.x, d.target.y]

      context.moveTo(sx, sy);
      context.lineTo(tx, ty);        
    })
    context.strokeStyle = "#aaa"
    context.stroke()

    context.beginPath()
    graph.nodes.forEach(d => {
      const [x, y] = [d.x, d.y]

      context.moveTo(x + 3, y);
      context.arc(x, y, 3, 0, 2 * Math.PI);        
    })
    context.fill()
    context.strokeStyle = "#fff"
    context.stroke()
  })
  function ticked() {
    path.forceUpdate(true)
  }

  function dragsubject() {
    const [x, y] = layer.toLocalPos(d3.event.x, d3.event.y)
    return simulation.find(x, y);
  }
});

function dragstarted() {
  if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  
  const [x, y] = [d3.event.subject.x, d3.event.subject.y]

  d3.event.subject.fx = x;
  d3.event.subject.fy = y;

  d3.event.subject.x0 = x;
  d3.event.subject.y0 = y;
}

function dragged() {
  const [x, y] = [d3.event.x, d3.event.y],
        {x0, y0} = d3.event.subject

  const [dx, dy] = layer.toLocalPos((x - x0), (y - y0))

  d3.event.subject.fx = x0 + dx;
  d3.event.subject.fy = y0 + dy;
}

function dragended() {
  if (!d3.event.active) simulation.alphaTarget(0);
  d3.event.subject.fx = null;
  d3.event.subject.fy = null;
}

  </script>
</body>
</html>